/**
 * 生成删除配置处理函数
 *
 * @param {(ids: string[]) => Promise<ResultType>} removeByIds 根据 id 批量删除配置的处理函数
 * @param {StoreType} store 状态存储仓库
 * @returns {(ids: (string | string[]), cascade: boolean) => Promise<void>}
 */
export const genDelConfsHandler = (
  removeByIds: (ids: string[]) => Promise<ResultType>,
  store: StoreType
): ((ids: string | string[], cascade: boolean, success?: () => void) => Promise<void>) => {
  const isDeleting = ref(false)

  const delConfsHandler = async (
    ids: string | string[],
    cascade: boolean,
    success: () => void = () => {}
  ) => {
    await ElLoadingGlobal({
      isLoading: () => isDeleting.value,
      handler: async () => {
        isDeleting.value = true
        const removeIdList: string[] = []
        if (ArrUtil.isArr(ids)) removeIdList.push(...ids)
        else removeIdList.push(ids)
        // 需要级联删除
        if (cascade) {
          const queue = [...removeIdList]
          while (queue.length > 0) {
            const id = queue.shift() as string
            ArrUtil.traverseDeep(store.getTreeMapAll()[id]?.children || [], {
              objPreHandler: (item) => removeIdList.push(item?._id)
            })
          }
        }
        const res = await removeByIds(removeIdList)
        await store.load()
        tipByResult(res)
        isDeleting.value = false
        if (res.status) success()
      },
      errHandler: (e: any) => {
        ElErrorNotification({
          title: '删除失败',
          message: e?.message || ''
        })
        isDeleting.value = false
      }
    })
  }

  return FunUtil.throttleByLimit(delConfsHandler)
}
